package com.ayuku.retail.ui.stock;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AyBigDecimal;
import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.Util;
import com.ayuku.retail.persistence.model.TrxTempSale;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 * 
 */
public class EditItem extends org.eclipse.swt.widgets.Dialog {

	private Shell dialogShell;
	private Label txtKode;
	private AyBigDecimal txtQty;
	private Label txtKeterangan;
	private String kode;

	public String getKode() {
		return kode;
	}

	public void setKode(String kode) {
		this.kode = kode;
	}

	/**
	 * Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
	 * inside a new Shell.
	 */
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			EditItem inst = new EditItem(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public EditItem(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM
					| SWT.APPLICATION_MODAL);
			dialogShell.setSize(450, 66);
			dialogShell.setLayout(new GridLayout(3, false));

			txtKode = new Label(dialogShell, SWT.NONE);
			txtKode.setText("Kode");

			txtKode.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));
			txtKode.setBackground(SWTResourceManager.getColor(192, 192, 192));

			txtKeterangan = new Label(dialogShell, SWT.NONE);
			txtKeterangan.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
					true, false, 1, 1));
			txtKeterangan.setText("Keterangan");

			txtKeterangan.setFont(SWTResourceManager.getFont("Courier", 12, 1,
					false, false));
			txtKeterangan.setBackground(SWTResourceManager.getColor(192, 192,
					192));

			txtQty = new AyBigDecimal(dialogShell, SWT.BORDER);
			GridData gd_txtQty = new GridData(SWT.LEFT, SWT.CENTER, false,
					false, 1, 1);
			gd_txtQty.widthHint = 67;
			txtQty.setLayoutData(gd_txtQty);

			txtQty.setBackground(SWTResourceManager.getColor(255, 255, 255));
			txtQty.setFont(SWTResourceManager.getFont("Courier", 12, 1, false,
					false));

			txtQty.getOrgComponent().getControl()
					.addKeyListener(new KeyAdapter() {
						public void keyPressed(KeyEvent evt) {
							txtQtyKeyPressed(evt);
						}
					});
			txtQty.setFocus();
			txtQty.getOrgComponent().getControl().selectAll();

			dialogShell.layout();

			Util.setSize(dialogShell, 500, 70);
			Util.setCenter(dialogShell);

			loadData();

			dialogShell.open();

			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void txtQtyKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {

			TrxTempSale sale;
			try {
				sale = AyukuDB.getPersist().readByPrimaryKey(TrxTempSale.class,
						txtKode.getText().trim());

				if (sale != null) {

					sale.setQty(txtQty.getValue());
					AyukuDB.getPersist().update(sale);

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			dialogShell.dispose();
		}
	}

	private void loadData() throws SQLException {
		TrxTempSale sale = AyukuDB.getPersist().readByPrimaryKey(
				TrxTempSale.class, this.kode);

		txtKode.setText(sale.getBarcode());
		txtKeterangan.setText(sale.getKeterangan());
		txtQty.setValue(sale.getQty());

		txtQty.getOrgComponent().getControl().setTextLimit(4);
		txtQty.getOrgComponent().getControl().selectAll();

	}

}
